const app = getApp()
var innerAudioContext
import filter from '../../mixins/filter'
import { HiddenCall } from '../../request/api'
const request = new HiddenCall
Page({
  mixins: [filter],
  data: {

  },
  onLoad: function (options) {
    let detail = JSON.parse(options.data)
    this.setData({ voice: detail.voice })
    if (detail.data.callId && detail.data.resourceCode) {
      let tactics = detail.data.utype === "house" ? "houseServiceImpl" :"customerServiceImpl"
      let resourceCode = detail.data.resourceCode
      request.queryDetail({ tactics, resourceCode}).then((res) => {
        this.setData({ detailData: res })
      })
    } else {
      this.setData({ detailData: detail.data })
    }
    // 播放通话录音
    innerAudioContext = wx.createInnerAudioContext()
    // 播放通话结束
    innerAudioContext.onPlay(() => {})
    innerAudioContext.onTimeUpdate(() => {
      var progress = parseInt((innerAudioContext.currentTime / innerAudioContext.duration) * 100)
      this.setData({ progress })
    })
    innerAudioContext.onEnded(() => {
      // 停止播放
      this.stopTelRecord()
      if (this.data.progress < 100) {
        this.setData({ progress: 100 })
      }
    })
    // 获取语音转的文字
    request.turnText({ url: this.data.voice.voiceUrl, duration: this.data.voice.duration}).then((res) => {
      if(res.statusCode === 1000){
        this.setData({ voiceText: res.result.text })
        this.getHeight()
      } else if (res.statusCode === 420505){
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 3000
        })
      }else{
        wx.showToast({
          title: '请稍后重试',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  onUnload() {
    // 停止播放
    this.stopTelRecord()
  },
  /**
     * 播放房客源详情通话录音
     * author wl
     */
  playTelRecord(e) {
    // 播放地址
    let url = e.currentTarget.dataset.url
    // 开始播放
    innerAudioContext.src = url
    innerAudioContext.play()
    this.setData({ play: true})
  },
  /**
 * 停止播放房客源详情通话录音
 * author wl
 */
  stopTelRecord(e) {
    // 停止播放
    innerAudioContext.stop()
    // 数据更新
    this.setData({ play: false })
  } 
})